import 'package:flutter/material.dart';
import 'package:flutter_common/widget/funAppBar.dart';
import 'package:flutter_common/net/http.dart';
import 'package:flutter_common/widget/loading.dart';
import 'package:flutter_common/widget/confirmDialog.dart';
import 'package:flutter_fun_app/model/musicRadioModel.dart';
import 'package:flutter_fun_app/model/radioModel.dart';
import 'package:flutter_common/widget/loadingAnim.dart';

class RadioListPage extends StatefulWidget {
  Channellist channel;

  RadioListPage({this.channel});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return RadioListPageState(channel: channel);
  }
}

class RadioListPageState extends State<RadioListPage> {
  Channellist channel;

  RadioListPageState({this.channel});

  List<Songlist> radioList = List();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getMusicRadio();
  }

  Future<void> getMusicRadio() async {
    var url = 'https://api.apiopen.top/musicBroadcastingDetails';
    var data = {'channelname': channel.chName};
    HttpUtil.get(url, data: data, success: (data) {
      MusicRadioModel radioModel = MusicRadioModel.fromJson(data);
      if (radioModel.code == 200) {
        radioModel.result.songlist.removeLast();
        setState(() {
          radioList = radioModel.result.songlist;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: FunAppBar(
        title: channel.cateSname + '-' + channel.name,
      ),
      body: radioList.length == 0
          ? LoadingAnim.getShimmerAnim()
          : GridView.builder(
              padding: EdgeInsets.all(5),
              itemCount: radioList.length,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 4,
                mainAxisSpacing: 3,
                crossAxisSpacing: 3,
                childAspectRatio: 0.85,
              ),
              itemBuilder: (BuildContext context, int index) {
                return radioItem(index);
              }),
    );
  }

  Widget radioItem(int index) {
    Songlist song = radioList[index];
    return GestureDetector(
      child: Column(
        children: <Widget>[
          Image.network(
            song.thumb,
            fit: BoxFit.fill,
          ),
          Text(
            song.title,
            style: TextStyle(color: Colors.grey[600], fontSize: 10),
            maxLines: 1,
          ),
        ],
      ),
      onTap: () {
//        ConfirmDialog.show(context, content: song.title);
        ConfirmDialog.show(context,
            title: 'title', content: song.title, cancel: () {}, confirm: () {});
      },
    );
  }
}
